﻿/**********************************************
* Class : StringUtils
* Version : 1.0 build 
*______________________________________________
*
* Author : Wissarut Pimanmassuriya (Draft)
* Mobile : 081-8338186
* Email : wissar_p@hotmail.com
* website : http://www.lifeztream.com
*______________________________________________
*
* Using : Static Class
* 
**********************************************/

package net.area80.utils {
	
	
	public class StringUtils {
		
		public static const day:Array = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
		public static const shortday:Array = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
		public static const month:Array = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
		public static const shortmonth:Array = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
		//
		public static const day_th:Array = new Array("อาทิตย์", "จันทร์", "อังคาร", "พุธ", "พฤหัสบดี", "ศุกร์", "เสาร์");
		public static const month_th:Array = new Array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
		
		/**
		* Add zero to string ex: your string is "3" then define result length to be it will returns "003".
		* @param	str
		* @param	resultLength
		* @return
		*/
		public static function addZero (num:Object, resultLength:Number):String {
				var str:String = String(num);
				var result:String = str;
				if(str.length <resultLength){
					var s:String = "";
					for(var i:uint=0;i<resultLength-str.length;i++){
						s += "0";
					}
					result = s+str;
				} 
				return result;
		}
		public static function moneyFormat (value:int):String {
			var res:String = "";
			var v:String = String(value);
			var count:uint = 0;
			for(var i:int =v.length-1;i>=0;i--,count++) {
				if(count%3==0 && count>0) {
					res = ","+res;
				}
				res = v.charAt(i)+res;
				
			}
			return res;
		}
		public static function trim(str:String):String {
			var res:String=str;
			while (res.charAt(0) == " ") {
				res = res.substr(1);
			}
			while (res.charAt(res.length-1) == " ") {
				res = res.substr(0,res.length-1);
			}
			return res;
		}
		public static function firstUpperCase (str:String):String {
			return String(str).substr(0,1).toUpperCase()+String(str).substr(1);
		}
		
		/**
		 * Return time string in the format 05:47:22
		 * @param	d
		 * @return
		 */
		public static function getTime (d:Date):String {
				return addZero(d.getHours(), 2) + ":" + addZero(d.getMinutes(), 2) + ":" + addZero(d.getSeconds(), 2);
		}
		public static function getThaiDate (d:Date):String {
				return addZero(d.getHours(), 2) + ":" + addZero(d.getMinutes(), 2) + " " + d.getDate() + " " + month_th[d.getMonth()] + " " + (d.getFullYear()+543);
		}
		public static function initHTMLString (s:String, doTrimming:Boolean = true):String {
			var str:String = (doTrimming) ? trim(s) : s;
			
			return str.split(String.fromCharCode(10)).join("").split(String.fromCharCode(13)).join("").split(String.fromCharCode(9)).join("");
		}
		public static function getEnDate (d:Date):String  {
				return addZero(d.getHours(), 2) + ":" + addZero(d.getMinutes(), 2) + " " + d.getDate() + " " + month[d.getMonth()] + " " + (d.getFullYear());
		}
	}
}